package dataPrp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

import linearRegression.featureExtractor.LinearRegression_tvt;

public class LoadFeature {
	public static String trainingSetPath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/training_set_new/";
	public static String mFeaturePath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/feature_load/m_f.txt";
	public static String uFeaturePath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/feature_load/u_f.txt";
	public static String testFile = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/qualifying.txt";
	public static String testFileOut = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/test_out_svd.txt";
	
	public float[][] mF;
	public float[][] uF;
	public Map<Integer, Integer> custMap;
	
	public final int fnum = 64;
	public LoadFeature(){
		mF = new float[17771][fnum];
		uF = new float[480191][fnum];
		custMap = new HashMap<Integer, Integer>();
	}
	
	public void loadF() throws Exception{
		BufferedReader br = new BufferedReader(new FileReader(mFeaturePath));
		
		String line = br.readLine();
		int index = 0;
		while ((line = br.readLine()) != null){
			index ++;
			String[] s = line.split(",");
			for (int i = 0; i < s.length; i++){
				mF[index][i] = Float.parseFloat(s[i]);
			}
		}
		
		br.close();
		
		br = new BufferedReader(new FileReader(uFeaturePath));
		br.readLine();
		index = 0;
		while ((line = br.readLine()) != null){
			index ++;
			String[] s = line.split(",");
			for (int i = 0; i < s.length; i++){
				uF[index][i] = Float.parseFloat(s[i]);
			}
		}
		br.close();
	}
	
	public void loadData(){
		File dir = new File(LinearRegression_tvt.trainingSetPath);
		File[] files = dir.listFiles();
		int index = 0;
		int userIndex = 1;
		for (File file : files){
			
			System.out.println(file.getName() +"\t" + index);
			try{
				BufferedReader br = new BufferedReader(new FileReader(file));
				
				String line;
				short movie = 0;
				int cust = 0;
				while ((line = br.readLine()) != null){
					if (line.endsWith(":"))
					{
						movie = Short.parseShort(line.substring(0, line.length()-1));
					}else{
						String[] s = line.split(",");
						cust = Integer.parseInt(s[0]);
						if (custMap.containsKey(cust)){
							cust = custMap.get(cust);
						}else{
							custMap.put(cust, userIndex);
							userIndex ++;
						}
						index = index + 1;
					}
				}
				
				
				br.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		System.out.println("Total Number\t" + index);
		System.out.println("Total Number\t" + userIndex);
	}
	
	public double normValue(double score){
		double temp = score;
		if (temp > 5) temp = 5;
		else if (temp < 1) temp = 1;
		return temp;
	}
	
	public double predictRate(int movie, int cust){
		double score = 1;
		for (int i = 0; i < fnum; i ++){
			score += mF[movie][i]*uF[cust][i];
			if (score > 5) score = 5;
			if (score < 1) score = 1;
		}
//		score = normValue(score);
		return score;
	}
	
	public void predictTest() throws Exception{
		BufferedReader br = new BufferedReader(new FileReader(LoadFeature.testFile));
		BufferedWriter bw = new BufferedWriter(new FileWriter(LoadFeature.testFileOut));
		
		String line;
		int movie = 0;
		int cust;
		int oldCust;
		float score;
		
		while((line = br.readLine()) != null){
			if (line.endsWith(":")){
				movie = Integer.parseInt(line.substring(0, line.length()-1));
			}else{
				oldCust = Integer.parseInt(line.substring(0, line.indexOf(",")));
				cust = custMap.get(oldCust);
				score = (float) predictRate(movie, cust);
				
				bw.write("" + oldCust + "," + movie + "," + score + "\n");
			}
		}
		
		br.close();
		bw.close();
	}
	
	public static void main(String[] args){
		LoadFeature engine = new LoadFeature();
		try{
			
			engine.loadF();
			System.out.println("after Loading F");
			engine.loadData();
			engine.predictTest();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
